3 Get environmental data
Data are generated in R/getDataEnv-targets.R file
3.1 Get environmental data target and show data frame structure
Code
envDataWB <- tar_read(envDataWB_Target)
str(envDataWB)tibble [32,189 × 22] (S3: tbl_df/tbl/data.frame)
$ river : chr [1:32189] "wb jimmy" "wb jimmy" "wb jimmy" "wb jimmy" ...
$ date : POSIXct[1:32189], format: "1997-05-14 04:00:00" "1997-05-15 04:00:00" ...
$ temperature : num [1:32189] 11.57 10.4 10.55 8.84 9.09 ...
$ daily_max_temp: num [1:32189] 11.6 12.3 11.9 10.2 11.6 ...
$ daily_min_temp: num [1:32189] 11.57 8.96 9.73 7.74 7.45 ...
$ flow : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
$ dateDate : Date[1:32189], format: "1997-05-14" "1997-05-15" ...
$ yday : num [1:32189] 134 135 136 137 138 139 140 141 142 143 ...
$ year : num [1:32189] 1997 1997 1997 1997 1997 ...
$ riverOrdered : Ord.factor w/ 4 levels "West Brook"<"WB Jimmy"<..: 2 2 2 2 2 2 2 2 2 2 ...
$ dayLength : num [1:32189] 51770 51896 52019 52140 52258 ...
$ precip : num [1:32189] 0 1.15 0 0 2.34 ...
$ solarRadiation: num [1:32189] 587 562 259 434 446 ...
$ swe : num [1:32189] 78.7 73.6 67.9 63 59.5 ...
$ airTempMax : num [1:32189] 20.1 21.2 12.7 15.1 18.9 ...
$ airTempMin : num [1:32189] 4.03 6.28 7.47 5.64 2.38 6.59 7.91 3.28 4.98 3.99 ...
$ vaporPressure : num [1:32189] 815 953 1034 911 725 ...
$ airTempMedian : num [1:32189] 12.1 13.7 10.1 10.4 10.6 ...
$ ETmm : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
$ Pmm : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
$ flowByRiver : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
$ flowByRiverm3s: num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
Code
ojs_define(envDataWB_OJS = envDataWB)3.2 Plot flow over time for the West Brook by year
Code
ggplot(envDataWB %>% filter(riverOrdered == "West Brook"), aes(yday, flow)) +
geom_point(size = 0.1) +
geom_point(size = 0.1, color = "blue", aes(yday, flowByRiverm3s)) +
#geom_line() +
scale_x_continuous("Day of year") +
scale_y_continuous("Stream flow (m^3/s)") +
facet_wrap(~year(date))
Code
#| label: fig-envFlowByRiver
#| fig-cap: "Stream flow (m3/s) for the West Brook with Jenn's model predictions"
ggplot(envDataWB %>% filter(riverOrdered == "West Brook", year %in% (1998:2021)), aes(flow, flowByRiverm3s, color = yday)) +
geom_point(size = 0.1) +
geom_abline(slope = 1) +
scale_y_continuous("Stream flow (m^3/s) from Jenn's model") +
scale_x_continuous("Stream flow (m^3/s) from flow extension") +
facet_wrap(~year(date))
Code
viewof rangeYear = Inputs.range([1997, 2022], {
label: "Which year?",
value: 2002,
step: 1
})
envDataWB_OJSplot = envDataWB_OJS.filter(d => d.year === rangeYear)Code
Plot.plot({
width: width,
height: 350,
inset: 10,
color: {
scheme: "greys"
},
x: { label: "Stream flow (m3/s)" },
y: { label: "Stream flow (m3/s) from Jenn's model" },
marks: [
Plot.frame(),
Plot.dot(envDataWB_OJSplot, {
x: "flow",
y: "flowByRiverm3s"
})
],
facet: {
data: envDataWB_OJSplot,
x: "riverOrdered"
}
});Test: referring to Figure 3.1.
3.3 Plot temperature over time for each tributary
Code
ggplot(envDataWB, aes(date, temperature)) +
geom_point(size = 0.2) +
scale_x_continuous("Date") +
scale_y_continuous("Stream temperature (C)") +
facet_wrap(~river)
3.4 Plot air and water temperature over time for each tributary
Code
getTempYear <- function(d, yearIn){
d %>% filter(year == yearIn)
}
tempYear <- getTempYear(envDataWB, 2002)
#write.csv(tempYear, "./data/tempYear.csv")
ojs_define(tempYearOJS0 = tempYear)
ggplot(tempYear, aes(date, temperature)) +
geom_point(size = 0.2) +
geom_point(aes(date, airTempMedian), color = "blue", size = 0.2) +
scale_x_continuous("Date") +
scale_y_continuous("Stream temperature (C)") +
facet_wrap(~river)
ggplot(tempYear, aes(airTempMedian, temperature, color = yday)) +
geom_point(size = 0.2) +
scale_x_continuous("Air Temperature") +
scale_y_continuous("Stream temperature (C)") +
facet_wrap(~river)

Will move this to its own chapter
Code
tempYearOJS = transpose(tempYearOJS0)
//tempYearOJS0
//tempYearOJS
dateChunksCode
dateChunks = {
let daysCount = rangeWindowWidth;
let days = [
{
daysCount0: 0,
start: tempYearOJS[0].dateDate,
end: tempYearOJS[rangeWindowWidth].dateDate,
daysCount: rangeWindowWidth,
data: tempYearOJS.filter(
(d) => d.yday >= 0 && d.yday <= 0 + rangeWindowWidth
)
}
];
do {
if (daysCount < 365) {
// dont push if the whole year is seleceted, array will be empty
days.push({
daysCount0: daysCount,
start: tempYearOJS[daysCount].dateDate,
end: tempYearOJS[daysCount + rangeWindowWidth].dateDate,
daysCount: daysCount,
data: tempYearOJS.filter(
(d) =>
d.yday >= daysCount + 1 && d.yday <= daysCount + rangeWindowWidth
)
});
}
daysCount = daysCount + rangeWindowWidth;
} while (daysCount <= 365);
return days;
}Use the slider below to set the length (# of days) of each data chunk.
Number of data chunks:
Start date =
end date =
Code
viewof rangeWindowWidth = Inputs.range([1, 365 - 0], {
label: "Data chunk length",
value: 14,
step: 1
})Of the data chunks defined above which one should we graph. 0 is the first one and you won’t be able to go past the last one (range updates automatically) but you might get as few as one observation in the last chunk.
Code
viewof rangeWhichWindow = Inputs.range([0, dateChunks.length - 1], {
label: "Which data chunk?",
step: 1,
value: 0
})Code
Plot.plot({
width: width,
height: 350,
inset: 10,
color: {
scheme: "greys"
},
x: { label: "Air temperature (C)" },
y: { label: "Water temperature (C)" },
marks: [
Plot.frame(),
Plot.dot(dateChunks[rangeWhichWindow].data, {
x: "airTempMedian",
y: "temperature",
stroke: "yday"
})//,
// Plot.linearRegressionY(dateChunks[rangeWhichWindow].data, {
// x: "airTempMedian",
// y: "temperature"
// })
],
facet: {
data: dateChunks[rangeWhichWindow].data,
x: "river"
}
})=======================================================
some integration alternatives - most are not so slick and are independent from the ojs code blocks above
Seems like it is best to devleop in observable and copy/paste code blocks in here.
import chunk from ‘Playground’
Code
import { plotChunk } from "@bletcher/Playground"
plotChunkiframe in html block
runtime with javascript in html block
Credit: Playground by Ben Letcher